package com.ctshk.rpc.custom.mapper;

import com.ctshk.rpc.custom.base.CustomBaseMapper;
import com.ctshk.rpc.custom.entity.CustomizedTripScheduleChannelDetail;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 团期线下渠道明细 Mapper 接口
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-03
 */
public interface CustomizedTripScheduleChannelDetailMapper extends CustomBaseMapper<CustomizedTripScheduleChannelDetail> {

    /**
     * 清空明细
     * @param relationId
     * @param userId
     * @return
     */
    @Update("UPDATE t_customized_trip_schedule_channel_detail \n" +
            "SET is_deleted = 1 AND modified_id = #{user_id}\n" +
            "WHERE relation_id = #{relation_id} AND is_deleted = 0")
    Integer cleanByRelationId(@Param("relation_id") Long relationId,@Param("user_id") Long userId);

    /**
     * 当前用户可查看团期ID列表
     * @param userId
     * @param deptId
     * @return
     */
    @Select("SELECT b.schedule_id \n" +
            "FROM t_customized_trip_schedule_channel_detail a\n" +
            "LEFT JOIN t_customized_trip_schedule_channel b ON a.channel_id = b.id\n" +
            "WHERE a.is_deleted = 0 AND a.check_type = 1 AND a.relation_id = #{dept_id}\n" +
            "OR (a.is_deleted = 0 AND a.check_type = 2 AND a.relation_id = #{user_id})")
    List<Long> queryScheduleListByUser(@Param("user_id") Long userId, @Param("dept_id") Long deptId);

    /**
     * 查询部门加幅
     * @param deptId
     * @param channelId
     * @return
     */
    @Select("SELECT * \n" +
            "FROM t_customized_trip_schedule_channel_detail\n" +
            "WHERE channel_id = #{channel_id} AND relation_id = #{relation_id} AND is_deleted = 0\n" +
            "ORDER BY gmt_create DESC LIMIT 1")
    CustomizedTripScheduleChannelDetail queryByDept(@Param("relation_id") Long deptId, @Param("channel_id") Long channelId);
}
